package com.yuandian.bpm.bi.modules.quartz.queue;

import com.yuandian.bpm.bi.modules.quartz.mapper.ConfReportFileInfoMapper;
import com.yuandian.bpm.bi.modules.quartz.service.HandelBaseService;
import com.yuandian.bpm.bi.modules.quartz.util.SpringContextJobUtil;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportFileInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.BlockingQueue;

/**
 * 异步保存文件信息
 */
@Slf4j
public class FileInfoStorage implements Runnable {

	private static ConfReportFileInfoMapper fileInfoMapper;

    private final BlockingQueue<ConfReportFileInfo> fileQueue;

    public FileInfoStorage(BlockingQueue<ConfReportFileInfo> fileQueue) { this.fileQueue = fileQueue; }


    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
				// 异步保存文件信息
				ConfReportFileInfo fileContent = fileQueue.take();
				saveFileInfo(fileContent);
            }
        } catch (InterruptedException e) {
            // 处理线程中断异常，可记录日志或执行其他操作
            Thread.currentThread().interrupt();
        }
    }



	/**
	 * 保存文件信息
	 * @param fileContent	文件信息对象
	 */
    private void saveFileInfo(ConfReportFileInfo fileContent) {
		try {
			if (fileInfoMapper == null) {
				fileInfoMapper = (ConfReportFileInfoMapper) SpringContextJobUtil.getBean("confReportFileInfoMapper");
			}
			fileInfoMapper.saveFileInfo(fileContent);
			log.info("[任务执行]-[任务id: {}]-保存邮件信息", fileContent.getTaskId());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[任务执行]-[任务id: {}]-保存邮件信息-报错", e.getMessage());
		}
    }
}
